<?php
/**
 * bll episode
 * @package app-drama_lib_bll
 */
load_lib('/bll/base');
load_lib('/dao/episodedao');
load_lib('/dao/importdao');
/**
 * bll episode
 * @author pandajingjing
 * @package app-drama_lib_bll
 */
class bll_episode extends bll_base{

    /**
	 * 获取没有导入系统的文件列表
	 * @return array
	 */
    function getUnImportList(){
        $arrData=dao_importdao::getImportList(array( 
                'DSTATUS'=>1,
                'SORT'=>'CREATETIME desc' 
        ));
        foreach($arrData as $intIndex=>$arrImport){
            $arrData[$intIndex]['EXT']=trim(substr(strrchr($arrImport['FILENAME'],'.'),1));
        }
        return $arrData;
    }

    /**
	 * 获取剧集语言列表
	 * @return array
	 */
    function getLangList(){
        return dao_episodedao::getLangList(array( 
                'SORT'=>'DORDER desc' 
        ));
    }

    /**
     * 添加剧集
     * @param string $p_strFileKey
     * @param int $p_intHostID
     * @param int $p_intSize
     * @param string $p_strExt
     * @param string $p_strFileName
     * @param int $p_intSeriesID
     * @param int $p_intRatioID
     * @param int $p_intDubLangID
     * @param int $p_intOrder
     * @return int/array
     */
    function addEp($p_strFileKey,$p_intHostID,$p_intSize,$p_strExt,$p_strFileName,$p_intSeriesID,$p_intRatioID,$p_intDubLangID,$p_intOrder){
        $arrErrInfo=$this->chkEpInfo($p_strFileKey,$p_intRatioID,$p_intDubLangID,$p_intOrder);
        if(!empty($arrErrInfo)){
            return $arrErrInfo;
        }
        $arrParam=array( 
                'FILEKEY'=>$p_strFileKey,
                'DSTATUS'=>2 
        );
        dao_importdao::updImportFile($arrParam);
        $arrParam=array( 
                'FILEKEY'=>$p_strFileKey,
                'HOSTID'=>$p_intHostID,
                'FILESIZE'=>$p_intSize,
                'EXT'=>$p_strExt,
                'FILENAME'=>$p_strFileName,
                'SERIESID'=>$p_intSeriesID,
                'RATIOID'=>$p_intRatioID,
                'DUBLANGID'=>$p_intDubLangID,
                'DORDER'=>$p_intOrder 
        );
        $intID=dao_episodedao::addEp($arrParam);
        $arrMsg=array( 
                'MSGTYPE'=>2,
                'SERIESID'=>$p_intSeriesID 
        );
        dao_importdao::addDramaQueue($arrMsg);
        return $intID;
    }

    /**
     * 编辑剧集
     * @param string $p_strFileKey
     * @param int $p_intRatioID
     * @param int $p_intDubLangID
     * @param int $p_intOrder
     */
    function editEp($p_strFileKey,$p_intRatioID,$p_intDubLangID,$p_intOrder){
        $arrErrInfo=$this->chkEpInfo($p_strFileKey,$p_intRatioID,$p_intDubLangID,$p_intOrder);
        if(!empty($arrErrInfo)){
            return $arrErrInfo;
        }
        $arrParam=array( 
                'FILEKEY'=>$p_strFileKey,
                'RATIOID'=>$p_intRatioID,
                'DUBLANGID'=>$p_intDubLangID,
                'DORDER'=>$p_intOrder 
        );
        return dao_episodedao::saveEp($arrParam);
    }

    /**
     * 检查剧集信息
     * @param string $p_strFileKey
     * @param int $p_intRatioID
     * @param int $p_intDubLangID
     * @param int $p_intOrder
     * @return array
     */
    function chkEpInfo($p_strFileKey,$p_intRatioID,$p_intDubLangID,$p_intOrder){
        load_lib('/util/string');
        if(true!==util_string::chkStrLength($p_strFileKey,32,32)){
            return array( 
                    'Sys_Error_FileKeyErr' 
            );
        }
        $arrErrInfo=array();
        load_lib('/util/method');
        $p_intRatioID=util_method::filterData($this->getRatioList(),'RATIOID',$p_intRatioID,0);
        if(0==$p_intRatioID){
            $arrErrInfo[]='Err_Logic_RatioID_NoValue';
        }
        $p_intDubLangID=util_method::filterData($this->getLangList(),'LANGID',$p_intDubLangID,0);
        if(0==$p_intDubLangID){
            $arrErrInfo[]='Err_Logic_DubLangID_NoValue';
        }
        if(!util_string::chkDataType($p_intOrder,'int')){
            $arrErrInfo[]='Err_Logic_Order_ErrType';
        }
        return $arrErrInfo;
    }

    /**
     * 搜索剧集
     * @param array $p_arrParam
     * @return array
     */
    function searchEpisode($p_arrParam){
        $arrData=dao_episodedao::getEpList($p_arrParam);
        $intCnt=count($arrData);
        if($intCnt>0){
            $arrRatioList=$this->getRatioList();
            $arrLangList=$this->getLangList();
            $arrOrder=array();
            for($i=0;$i<$intCnt;$i++){
                foreach($arrRatioList as $arrRatio){
                    if($arrData[$i]['RATIOID']==$arrRatio['RATIOID']){
                        $arrData[$i]['RATIOSTR']=$arrRatio['CNAME'];
                    }
                }
                foreach($arrLangList as $arrLang){
                    if($arrData[$i]['DUBLANGID']==$arrLang['LANGID']){
                        $arrData[$i]['DUBLANGSTR']=$arrLang['CNAME'];
                    }
                }
                $arrOrder[]=$arrData[$i]['DORDER'];
            }
            $intBigOrder=max($arrOrder);
            $intSuppLen=strlen($intBigOrder);
            if(1==$intSuppLen){
                $intSuppLen=2;
            }
            $strSupp=str_repeat(0,$intSuppLen);
            $intSuppCut=0-$intSuppLen;
            for($i=0;$i<$intCnt;$i++){
                $arrData[$i]['ORDERSTR']=substr($strSupp.$arrData[$i]['DORDER'],$intSuppCut);
            }
        }
        return $arrData;
    }

    /**
     * 发送文件到终端
     * @param int $p_intSeriesID
     * @param string $p_strFileKey
     * @param int $p_intTerminalID
     */
    function sendEpTerminal($p_intSeriesID,$p_strFileKey,$p_intTerminalID){
        load_lib('/dao/terminaldao');
        load_lib('/dao/seriesdao');
        load_lib('/dao/taskdao');
        $arrTerminalList=dao_terminaldao::getTerminalList();
        $arrSeries=dao_seriesdao::getSeriesDetail($p_intSeriesID);
        $arrEpList=dao_episodedao::getEps($p_strFileKey);
        $arrRatioList=$this->getRatioList();
        $arrLangList=$this->getLangList();
        $arrTerminal=array();
        foreach($arrTerminalList as $arrTer){
            if($arrTer['code']==$p_intTerminalID){
                $arrTerminal=$arrTer;
                break;
            }
        }
        $intCnt=count($arrEpList);
        for($i=0;$i<$intCnt;$i++){
            $arrOrder[]=$arrEpList[$i]['DORDER'];
        }
        $intBigOrder=max($arrOrder);
        $intSuppLen=strlen($intBigOrder);
        if(1==$intSuppLen){
            $intSuppLen=2;
        }
        $strSupp=str_repeat(0,$intSuppLen);
        $intSuppCut=0-$intSuppLen;
        for($i=0;$i<$intCnt;$i++){
            $arrEpList[$i]['ORDERSTR']=substr($strSupp.$arrEpList[$i]['DORDER'],$intSuppCut);
        }
        
        foreach($arrEpList as $arrEp){
            foreach($arrRatioList as $arrRatio){
                if($arrEp['RATIOID']==$arrRatio['RATIOID']){
                    $arrEp['RATIOSTR']=$arrRatio['CNAME'];
                    break;
                }
            }
            foreach($arrLangList as $arrLang){
                if($arrEp['DUBLANGID']==$arrLang['LANGID']){
                    $arrEp['DUBLANGSTR']=$arrLang['CNAME'];
                    break;
                }
            }
            $strSubDir=$this->dispatchFile($arrEp['FILEKEY']);
            $strBaseDir=dao_episodedao::getConfig('storage','storage');
            $strDir=$strBaseDir.DIRECTORY_SEPARATOR.$arrEp['HOSTID'].$strSubDir;
            $strFilePath=$strDir.$arrEp['FILEKEY'];
            $strDestFileName=$arrTerminal['path'].DIRECTORY_SEPARATOR.'['.$arrSeries['CNAME'].']['.$arrSeries['ENAME'].']['.$arrEp['RATIOSTR'].']['.$arrEp['DUBLANGSTR'].']['.$arrEp['ORDERSTR'].'].'.$arrEp['EXT'];
            $arrQueue=array( 
                    'QTYPE'=>2,
                    'QDATA'=>json_encode(array( 
                            'TERMINALNAME'=>$arrTerminal['name'],
                            'SIZE'=>$arrEp['FILESIZE'],
                            'FROMPATH'=>$strFilePath,
                            'SERNAME'=>$arrSeries['CNAME'],
                            'ORDERSTR'=>$arrEp['ORDERSTR'],
                            'DESTPATH'=>$strDestFileName 
                    )) 
            );
            $intQueueID=dao_taskdao::addShowQueue($arrQueue);
            $arrMsg=array( 
                    'MSGTYPE'=>3,
                    'FROMPATH'=>$strFilePath,
                    'DESTPATH'=>$strDestFileName,
                    'QUEUEID'=>$intQueueID 
            );
            dao_taskdao::addDramaQueue($arrMsg);
        }
    }
}